package com.kmvc.message.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kmvc.message.R;
import com.kmvc.message.adapter.ViewPagerAdapter;

public class MessageGuide extends Activity implements OnTouchListener, OnPageChangeListener, OnClickListener {

	public Context context = MessageGuide.this;
	private ViewPager viewPager;

	private ViewPagerAdapter viewPagerAdapter;
	private List<View> views;
	private ImageView[] dots;
	private int currentIndex;
	private static final int[] pics = { R.drawable.guidefirst,
			R.drawable.guidesecond, R.drawable.guidethree,R.drawable.guidefour };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		SharedPreferences sPreferences=getSharedPreferences("isguide", 0);
		if (sPreferences.getBoolean("isguide", true)==true) {			
			Editor editor=sPreferences.edit();
			editor.putBoolean("isguide", false);
			editor.commit();
			setContentView(R.layout.guide);
			views = new ArrayList<View>();
			
			for (int i = 0; i < pics.length; i++) {
				ImageView iv = new ImageView(this);			
				iv.setBackgroundResource(pics[i]);
				views.add(iv);
			}
			viewPager = (ViewPager) findViewById(R.id.viewpager);
			viewPagerAdapter = new ViewPagerAdapter(views);
			viewPager.setOnTouchListener(this);
			viewPager.setAdapter(viewPagerAdapter);
			viewPager.setOnPageChangeListener(this);

			initBottomDots();
		}
		else {
			Intent intent = new Intent(context,LoginActivity.class);
			context.startActivity(intent);
			this.finish();		
		}
		
	}

	private void initBottomDots() {
		// TODO Auto-generated method stub
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
		dots = new ImageView[pics.length];
		for (int i = 0; i < pics.length; i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);
			dots[i].setOnClickListener(this);
			dots[i].setTag(i);
		}
		currentIndex = 0;
		dots[currentIndex].setEnabled(false);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction()) {
		 
		case MotionEvent.ACTION_MOVE:
			int i = views.size() - 1;
			if (currentIndex == i) {
				
				Intent intent = new Intent();
				intent.setClass(context, LoginActivity.class);
				startActivity(intent);				 
				this.finish();				 
			}
			break;
		default:
			break;
		}
		return false;
	
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int index) {
		// TODO Auto-generated method stub
		setCurDot(index);
	}

	private void setCurDot(int positon) {
		// TODO Auto-generated method stub
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}
		dots[positon].setEnabled(false);
		dots[currentIndex].setEnabled(true);
		currentIndex = positon;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int position = (Integer) v.getTag();
		setCurView(position);
		setCurDot(position);
	}

	private void setCurView(int position) {
		// TODO Auto-generated method stub
		if (position < 0 || position >= pics.length) {
			return;
		}
		viewPager.setCurrentItem(position);
	}

}
